package pers.gxm.abstractfactory.listfactory;

import pers.gxm.abstractfactory.factory.*;

import java.util.Iterator;

public class ListPage extends Page {
    public static void main(String[] args) {
//        ListFactory
        Factory listFactory = Factory.getFactory("pers.gxm.abstractfactory.listfactory.ListFactory");
        Link people = listFactory.createLink("人民日报", "http://www.people.com.cn/");
        Link gmw = listFactory.createLink("光明日报", "http://www.gmw.cn/");
        Link us_yahoo = listFactory.createLink("Yahoo!", "http://www.yahoo.com/");
        Link jp_yahoo = listFactory.createLink("Yahoo!Japan", "http://www.yahoo.co.jp/");
        Link excite = listFactory.createLink("Excite", "http://www.excite.cn/");
        Link google = listFactory.createLink("Google", "http://www.google.cn/");

        Tray traynews = listFactory.createTray("日报");
        traynews.add(people);
        traynews.add(gmw);

        Tray tray = listFactory.createTray("Yahoo!");
        tray.add(us_yahoo);
        tray.add(jp_yahoo);

        Tray traySearch = listFactory.createTray("搜索引擎");
        traySearch.add(tray);
        traySearch.add(excite);
        traySearch.add(google);
        Page page = listFactory.createPage("LinkPage", "杨文轩");
        page.add(traynews);
        page.add(traySearch);
        page.output();
        System.out.println(page.makeHTML());
    }
    public ListPage(String title,String author ){
        super(title, author);
    }
    @Override
    public String makeHTML() {
        StringBuilder sb = new StringBuilder();
        sb.append("<html>\n\t<head>\n\t\t<title>");
        sb.append(title);
        sb.append("</title>\n\t\t<meta charset=\"utf-8\">\n\t</head>\n\t<body>\n\t\t<h1>");
        sb.append(title);
        sb.append("</h1>\n");

        sb.append("\t\t<ul>\n");


        Iterator<Item> it = content.iterator();
        while(it.hasNext()){
            Item item = it.next();
            sb.append(item.makeHTML());
        }

        sb.append("\t\t</ul>\n");

        sb.append("\t\t<hr><address>");
        sb.append(author);
        sb.append("</address>\n\t</body>\n</html>");
        return sb.toString();
    }
}
